var $j = jQuery.noConflict();
$j(document).ready( function(){ 
	$j(".cb-enable").click(function(){
		var parent = $j(this).parents('.switch');
		$j('.cb-disable',parent).removeClass('selected');
		$j(this).addClass('selected');
		$j('#secondLeg').attr('checked', true);
	});
	$j(".cb-disable").click(function(){
		var parent = $j(this).parents('.switch');
		$j('.cb-enable',parent).removeClass('selected');
		$j(this).addClass('selected');
		$j('#secondLeg').attr('checked', false);
	});	
	
	$j('#tournamentPlayersAmount').live('change', function() {
		$j('#playerNames tbody>tr').remove();
		var playerAmount = $j('#tournamentPlayersAmount').val();
		if(playerAmount < 3 && playerAmount != '') {
			$j('#playerNames tbody').append('<tr class=\'rowA\'><td class=\'first\'>1</td><td><input type="text" value="Spieler 1" name="tx_romtectourngen_pi1[record][player_1]" maxlength="35"></td><td class=\'last\'><input type="text" value="Team 1" name="tx_romtectourngen_pi1[record][team_1]" maxlength="35"></td></tr><tr class=\'rowB\'><td class=\'first\'>2</td><td><input type="text" value="Spieler 2" name="tx_romtectourngen_pi1[record][player_2]" maxlength="35"></td><td class=\'last\'><input type="text" value="Team 1" name="tx_romtectourngen_pi1[record][team_1]" maxlength="35"></td></tr><tr class=\'rowA\'><td class=\'first\'>3</td><td><input type="text" value="Spieler 3" name="tx_romtectourngen_pi1[record][player_3]" maxlength="35"></td><td class=\'last\'><input type="text" value="Team 1" name="tx_romtectourngen_pi1[record][team_3]" maxlength="35"></td></tr>');
			alert("error");
		}else{
			var inputToAdd = [];
			var i = 0;
			for (var a = 0; a <playerAmount; a += 1) {
			inputToAdd[i++] = '<tr class=\'\'><td class=\'first\'>'+i+'</td><td><input type="text" value="Spieler '+i+'" name="tx_romtectourngen_pi1[record][player_'+i+']" maxlength="35"></td><td class=\'last\'><input type="text" value="Team 1" name="tx_romtectourngen_pi1[record][team_'+i+']" maxlength="35"></td></tr>';
			};
			$j('#playerNames tbody').append(inputToAdd.join(''));
			$j('#playerNames tbody tr:even').addClass('rowA');
			$j('#playerNames tbody tr:odd').addClass('rowB');
		}
	});
//ACCORDION
$j('table.roundRobin tr.statetablerow th').parents('table.roundRobin').children('tbody,tfoot').toggle();
$j('table.roundRobin tr.statetablerow th.first') .click(
	function() {
		$j(this).parents('table.roundRobin').children('tbody,tfoot').toggle(); 
		if ($j(this).html()== '+') {
			$j(this).html('-');
		}else{
			$j(this).html('+');
		}
	})
$j('.all').click(function() {
	$j('table.roundRobin').children('tbody,tfoot').toggle();
	$j('#rank').children('tbody,tfoot').toggle();
	if ($j('.all').html()== '+ alle öffnen') {
		$j('.all').html('- alle schliessen');
	}else{
		$j('.all').html('+ alle öffnen');
	}
});
$('#playerNames').click(function(event) {
	event.preventDefault();
	$('#playerNames').shuffleRows(); 
});
});

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }

  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
    //Shuffle all rows, don't tpuch first column
    //Requires: Shuffle
 $.fn.shuffleRows = function(){
     return this.each(function(){
        var main = $(/table/i.test(this.tagName) ? this.tBodies[0] : this);
        var firstElem = [], counter=0;
        main.children().each(function(){
             firstElem.push(this.firstChild);
         });
        main.shuffle();
        main.children().each(function(){
           this.insertBefore(firstElem[counter++], this.firstChild);
        });
     });
}
     })(jQuery)